package ru.jarda.model.services;


import ru.jarda.model.dao.Dao;
import ru.jarda.model.entities.Category;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by user on 14.02.2015.
 */

public class CategoryService {
    private Dao<Category> categories;

    public void setCategories(Dao<Category> categories) {
        this.categories = categories;
    }

    public List<Category> getAll(){

        return categories.getAllObjects();
    }

    public void add(String name,long parentId) {

        isDubleName(name, -1L , parentId);

        int order = getNextOrderInCategory(parentId);

        categories.saveObject(new Category(-1L, name,parentId,order));
    }

    private int getNextOrderInCategory(long id){
                int result=0;
                for (Category category:categories.getAllObjects()){
                    if (category.getParentId()==id) {
                        result = Math.max(result, category.getOrder());
                    }
        }
      return result+1;
    }

    public void delete(long id){
        Category category=categories.getObject(id);
        long parentId=category.getParentId();

        List<Category> brothersList = getChildren(parentId);
        List<Category> childrenList = getChildren(id);
        for (Category c:childrenList){
            isDubleName(c.getName(),c.getId(),parentId);
        }
        Collections.sort(brothersList);
        Collections.sort(childrenList);
        int position = brothersList.indexOf(category);
        brothersList.remove(position);
        brothersList.addAll(position,childrenList);
        for (Category c:childrenList){
            c.setParentId(parentId);
        }
        int i=1;
        for (Category c:brothersList){
            c.setOrder(i++);
            categories.editObject(c);
        }

        categories.deleteObject(id);

    }
    private List<Category> getChildren(long id){
        ArrayList<Category> result = new ArrayList<>();
        for (Category category:getAll()){
            if (category.getParentId()==id) {
                result.add(category);
            }
        }
        return result;
    }

    public void edit(long id,String name,long parentId){
        Category category=categories.getObject(id);
        long oldParentId=category.getParentId();
        isDubleName(name,id,parentId);
        category.setName(name);


        if (oldParentId!=parentId){
            category.setOrder(getNextOrderInCategory(parentId));
            category.setParentId(parentId);

            List<Category> brothersList = getChildren(oldParentId);
            int i=1;
            for (Category c:brothersList){
                c.setOrder(i++);
                categories.editObject(c);
            }

        }
        categories.editObject(category);
    }

    private long getKey(String name, long parentId){

        for (Category category:getChildren(parentId)) {
            if (category.getName().equals(name)) {
                return category.getId();
            }
        }
        return -1L;
    }
    private void isDubleName(String name,long id, long parentId){
        long findId=getKey(name, parentId);
        if (findId<0) return;
        if (findId!=id){
            throw new RuntimeException(" ");
        }

    }

}